/**
 * 刮刮卡
 * @author rubekid
 */
function Scratch(options){
	this.init(options);
}

Scratch.prototype = {
	init:function(opts){
		opts = opts || {};
		this.opts = opts;
		this.wrapper = opts.wrapper;
		this.maskLayerColor = opts.maskLayerColor || "#CCCCCC";
		this.maskLayerImage = opts.maskLayerImage;
		this.create();
		this.bindEvent();
	},
	create:function(){
		this.width = this.wrapper.innerWidth || this.wrapper.clientWidth;
		this.height = this.wrapper.innerHeight || this.wrapper.clientHeight;
		this.canvas = document.createElement("canvas");
		this.canvas.width = this.width;
		this.canvas.height = this.height;
		this.wrapper.appendChild(this.canvas);
		this.context = this.canvas.getContext("2d");
	},
	start:function(){
		if(this.maskLayerImage){
			var image = new Image();
			var _this = this;
			image.onload = function(){
				_this.context.drawImage(image, 0, 0, _this.width, _this.height);
			};
			image.src = this.maskLayerImage;
		}
		else{
			this.context.fillStyle = this.maskLayerColor;
			this.context.fillRect(0, 0, this.width, this.height);
		}
	},
	clear:function(){
		this.context.clearRect(0, 0, this.width, this.height);
	},
	bindEvent:function(){
		var _this = this;
		this.canvas.onmousemove  = function(e){
			_this.lottery(e);
		};
		
		this.canvas.ontouchmove = function(e){
			
			_this.lottery(e);
		};
	},
	lottery:function(event){
		var pageX = event.pageX || event.clientX;
		var pageY = event.pageY || event.clientY;
		var x = pageX - this.canvas.offsetLeft;
		var y = pageY - this.canvas.offsetTop;
		var r = this.opts.radius ||  parseInt(Math.random() * 5);
		
		this.context.globalCompositeOperation = 'destination-out';
		this.context.beginPath()
		this.context.arc(x, y, r, 0, Math.PI * 2);
		this.context.closePath();
		this.context.fill();
	}
};